<?php
class Categories_list extends Widget
{

	function run($categoria = NULL) {
		$cache_time = $this->settings->item("_site_cache_categories_time");
		$cache_time_nodes = $this->settings->item("_site_cache_nodes_time");
		
    	$data = array();
    	$this->load->model("Category_Model", "Categories");
    	
    	//TODO Problema en _getTree, ya que no almacenamos parent_name...
    	if(!is_numeric($categoria)){
    		$tmpCategoria = $this->mp_cache->model("Categories", "get", array(array("url_name" => $categoria, "status" => "all")), $cache_time);
    		if($tmpCategoria != NULL){
    			$categoria = $tmpCategoria->id;
    		}
    	}
    	
    	$params = array();
    	if($categoria != NULL){
    		$params['parent'] = $categoria;
    	}
    	$categories = $this->mp_cache->model("Categories", "get", array($params), $cache_time);
		
		$categories_process = array();
		
		foreach($categories as $category){
			$category_metas = $this->mp_cache->model("Categories", "getMeta", array("id" => $category->id), $cache_time);
			$category->metas = $category_metas;
			
			$category_num_nodes = $this->mp_cache->model("Nodes", "getCount", array(array("categories" => $category->id)), $cache_time_nodes);
			$category->num_nodes = $category_num_nodes;
			
			array_push($categories_process, $category);
		}
		
		$data['categories'] = $categories_process;
    			
		$this->template->load_clear("widgets/categories_list", $data);
    }
    
	
} 
?>